
//init state

const state = () => ({
  param:[
    /*{comId: 2,proNum: 1,isSelect: false},
    {comId: 3,serviceId: 3,proNum: 1,isSelect: false},
    {comId: 4,proNum: 1,isSelect: false},*/
  ]
})

//getters
const getters = {
  //为购物车提供数据
  giveDataShoppingCar(state,getters,rootState,rootGetters){
    let temp = state.param
    for (let paramKey in temp) {
      let tempVal = rootGetters["DetailData/getComImgAndComPrice"](temp[paramKey].comId);
      temp[paramKey] = {...temp[paramKey],...tempVal}
      let serviceName = rootGetters["ServiceMsg/getSerNameBySerId"](temp[paramKey].serviceId);
      if (serviceName){
        temp[paramKey] = {...temp[paramKey],...{serviceName: serviceName}}
      } else {
        temp[paramKey] = {...temp[paramKey],...{serviceName: null}}
      }
    }
    return state.param;
  },
  //获取总价格
  getTotalPriceMethod(state,getters,rootState,rootGetters){
    //取出所有选中商品对象
    let arr = state.param.filter(item => {
      return item.isSelect == true;
    });
    let totalPrice = arr.reduce((pre,next) => {
      let serPrices = rootGetters["ServiceMsg/getSerPriceBySerId"](next.serviceId);
      //计算出保修编号的价格
      let serPrice = parseInt((serPrices == undefined ? 0 : serPrices));
      return pre + (rootGetters["DetailData/getComImgAndComPrice"](next.comId).comPrice * next.proNum)
        +  serPrice;
    },0);
    return totalPrice;
  },
  getParamLengthIfNull(state){
    return state.param.length == 0;
  },
  refreshShopCarSessionStorageData(state){
    sessionStorage.setItem("ShopCarData",JSON.stringify(state.param));
  },
  getDataForOrderSubmitData(state){//将购物车中选择的数据提取出来，并且将其格式修改为订单提交页所能接受的数据
    let tempVal = [];
    state.param.filter(item => {
      if (item.isSelect){
        tempVal.push({
          comId: item.comId,
          buyNum: item.proNum,
          serviceId: item.serviceId
        })
      }
    });
    return tempVal;
  }
}

//actions
const actions = {
  updateIsSelect({commit},payload){//修改选中状态的方法
    commit("updateIsSelectMut",payload);
  },
  updateBuyNum({commit},payload){//修改购买数量的方法
    commit("updateBuyNumMut",payload);
  },
  deleteShoppingPro({commit},payload){//删除购物车商品
    return new Promise(resolve => {
      commit("deleteShoppingProMut",payload)
      resolve(true);
    });
  },
  loadShopCarData({commit},payload){//替换所有数据的方法
    commit("loadShopCarDataMut",payload);
  },
  addSingleVal({commit},payload){
    commit("addSingleValMut",payload);
  }
}

//mutations
const mutations = {
  updateIsSelectMut(state,comId){//修改选中状态的方法
    let shopping = state.param.filter(item => item.comId == comId)[0];
    shopping.isSelect = !shopping.isSelect;
  },
  updateBuyNumMut(state, payload) {//修改购买数量的方法
    let shopping = state.param.filter(item => {
      return item.comId == payload.comId;
    })[0];
    shopping.proNum = payload.currVal;
  },
  deleteShoppingProMut(state,comId){//删除购物车商品
    let index = 0;
    state.param.filter(item => {
      if (item.comId == comId) state.param.splice(index,1);
      ++index;
    });
  },
  loadShopCarDataMut(state,val){//替换所有数据的方法
    state.param = [...val];
  },
  addSingleValMut(state,val){
    state.param.push(val)
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
